import java.math.BigDecimal;

/**
 * 水果父类
 *
 * @author fy
 * @date 2022/8/23 21:14
 */
public abstract class Fruit {
    /**
     * 水果名
     */
    private String name;

    /**
     * 价格
     */
    private double price;

    /**
     * 活动价格策略
     */
    private PriceStrategy strategy = new DefaultStrategy();

    public Fruit(String name, double price) {
        this.name = name;
        this.price = price;
    }

    /**
     * 计算价格
     *
     * @param weight 重量
     * @return
     */
    public double calcPrice(double weight) {
        if (weight < 0) {
            throw new IllegalArgumentException("weight: " + weight + "重量必须为大于等于0");
        }
        double originalPrice = BigDecimal.valueOf(this.price).multiply(BigDecimal.valueOf(weight)).doubleValue();
        return this.strategy.calcPrice(originalPrice);
    }

    public void setStrategy(PriceStrategy strategy) {
        this.strategy = strategy;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return name + ": " + price + " 元/斤";
    }

}
